package com.ni.learning.exam.controller;

import com.ni.learning.exam.dto.EnterExamDTO;
import com.ni.learning.exam.dto.ExamDTO;
import com.ni.learning.exam.service.ExamService;
import com.our.learning.infrastructure.pojo.Response;
import org.springframework.web.bind.annotation.*;

/**
 * @author : Ice'Clean
 * @date : 2023-06-07
 */
@RestController
@RequestMapping("/exam")
public class ExamController {

    private final ExamService examService;

    public ExamController(ExamService examService) {
        this.examService = examService;
    }

    /**
     * 根据考试 ID 查询考试详情
     * @param examId 考试 ID
     * @return 考试详情
     */
    @GetMapping("/{examId}")
    public Response getExam(@PathVariable int examId) {
        return examService.getExam(examId);
    }

    /**
     * 创建一个考试
     * @param examDTO 考试数据
     * @return 考试 ID
     */
    @PostMapping
    public Response createExam(ExamDTO examDTO) {
        return examService.createExam(examDTO);
    }

    /**
     * 修改考试信息
     * @param examDTO 发生修改的考试信息
     * @return 是否修改成功
     */
    @PatchMapping
    public Response modifyExam(ExamDTO examDTO) {
        return examService.modifyExam(examDTO);
    }

    /**
     * 删除一个考试
     * @param examId 考试 ID
     * @return 是否删除成功
     */
    @DeleteMapping
    public Response deleteExam(int examId) {
        return examService.deleteExam(examId);
    }

    /**
     * 学生获取考试列表
     * @param studentId 学生 ID
     * @return 考试列表
     */
    @GetMapping("/student/list/{studentId}")
    public Response studentExamList(@PathVariable int studentId, int page, int perPage) {
        return examService.studentExamList(studentId, page, perPage);
    }

    /**
     * 老师获取考试列表
     * @param teacherId 老师 ID
     * @param page 页数
     * @param perPage 每页数量
     * @return 考试列表
     */
    @GetMapping("/teacher/list/{teacherId}")
    public Response teacherExamList(@PathVariable int teacherId, int page, int perPage) {
        return examService.teacherExamList(teacherId, page, perPage);
    }

    /**
     * 学生进入考试请求
     * @param enterExamDTO 进入考试信息
     * @return 分配答卷
     */
    @PostMapping("/enter")
    public Response enterExam(@RequestBody EnterExamDTO enterExamDTO) {
        return examService.enterExam(enterExamDTO, false);
    }

    /**
     * 教师进入考试请求
     * @param enterExamDTO 进入考试信息
     * @return 答卷
     */
    @PostMapping("/teacher/enter")
    public Response teacherEnterExam(@RequestBody EnterExamDTO enterExamDTO) {
        return examService.enterExam(enterExamDTO, true);
    }

    /**
     * 公布考试结果
     * @param examId 考试 ID
     * @return 公布反馈
     */
    @PostMapping("/publish")
    public Response publishResult(int examId) {
        return examService.publishResult(examId);
    }
}
